<?php


namespace app\common\model;

use think\Db;
use think\Exception;
use think\Model;

class Address extends Model
{
    // 表名
    protected $name = 'address';
    // 开启自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = '';
    // 追加属性
    protected $append = [
    ];

    /**
     * @var null|static 实例对象
     */
    protected static $instance = null;

    protected function getImageAttr($value)
    {
        return $value ? explode(',', $value) : null;
    }

    /**
     * 获取示例
     * @param array $options 实例配置
     * @return static
     */
    public static function instance($options = [])
    {
        if (is_null(self::$instance)) self::$instance = new self($options);

        return self::$instance;
    }

    /**
     * 添加地址
     * @param array $params
     * @param int $uid
     * @return bool
     * @throws
     */
    public function add(array $params, int $uid): bool
    {
        Db::startTrans();
        try {
            $params['user_id'] = $uid;
            $params['createtime'] = date('Y-m-d H:i:s');
            $result = $this->allowField(true)->save($params);
            if ($result) {
                Db::commit();
                return true;
            }
            throw new Exception('添加失败');
        } catch (Exception $exception) {
            Db::rollback();
            throw new Exception($exception->getMessage());
        }
    }

    /**
     * 编辑地址
     * @param array $params
     * @param int $uid
     * @return bool
     * @throws
     */
    public function editAddress(array $params, int $uid): bool
    {
        if (!$rows = $this->get(['id' => $params['id']])) {
            throw new Exception('未找到数据');
        }
        Db::startTrans();
        try {
            $params['user_id'] = $uid;
            $params['updatetime'] = date('Y-m-d H:i:s');
            unset($params['id']);
            $result = $rows->allowField(true)->save($params);
            if ($result) {
                Db::commit();
                return true;
            }
            throw new Exception('修改失败');
        } catch (Exception $exception) {
            Db::rollback();
            throw new Exception($exception->getMessage());
        }
    }

    /**
     * 删除地址
     * @param array $params
     * @return bool
     * @throws
     */
    public function delAddress(array $params): bool
    {
        if (!$rows = $this->get(['id' => $params['id']])) {
            throw new Exception('未找到数据');
        }
        Db::startTrans();
        try {
            $response = $rows->delete();
            if ($response) {
                Db::commit();
                return true;
            }
            throw new Exception('删除失败');
        } catch (Exception $exception) {
            Db::rollback();
            throw new Exception($exception->getMessage());
        }
    }

    public function userSignUp()
    {
        return $this->hasMany(SignUp::class, 'activity_id', 'id')
            ->field('user_id, activity_id');
    }
}